﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using DbDao;
using MediaFactory;
using Microsoft.Win32;

namespace ProjetDevExia
{
    public partial class MainWindow : Window, IDisposable
    {
        private I_Media media;
        private readonly I_OpenDialog openfileDialog;
        private MediaElement mediaPreview;
        private List<MediaElement> elsList; 
        private I_MediaDAO mediaDao;

        public MainWindow()
        {
            this.elsList = new List<MediaElement>();
            InitializeComponent();
            this.mediaDao = new MediaDao();
            this.openfileDialog = new OpenDialog();
            this.openfileDialog.openFileDialog.FileOk += OpenFileDialog_FileOk;
        }

        private void OpenFileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var openfile = sender as OpenFileDialog;

            if (this.elsList.Count == 1)
            {
                if ( this.elsList[0].Source.Equals( openfile.FileName ) ) { return; }
                this.Dispose();
            }
            //this.elsList[0] = null;
            this.elsList.Clear();
            this.MediaPreview.Children.Clear();
            this.media = new VideoFactory().CreateMedia(this.openfileDialog.openFileDialog.FileName);
            this.createPreview();
        }

        private void createPreview()
        {
            this.createViewPreview();
            this.VideoNameLable.Content = this.media.Path;
            this.setDuration(true, this.elsList[0]);
            this.SizeLableVal.Content = this.media.Size + " Octets";
            this.PathLableVal.Content = this.openfileDialog.openFileDialog.SafeFileName;
        }

        private void createViewPreview()
        {
            this.elsList.Add(new MediaViewCreator().getMediaView(this.media, MediaPreview.Width, MediaPreview.Height));
            this.MediaPreview.Children.Add( elsList[0] );
            //mediaPreview.MediaFailed += MediaPreview_MediaFailed;
            /*
            while (!this.mediaPreview.IsLoaded)
                
            this.mediaPreview.Play();
            this.mediaPreview.Pause();
            */
        }

        private void MediaPreview_MediaFailed(object sender, ExceptionRoutedEventArgs e)
        {
            
        }

        private void SelectVideoBtn_Click(object sender, RoutedEventArgs e)
        {
            this.openfileDialog.openFileDialog.ShowDialog();
        }

        private void SaveMediaBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.media.Name = this.PathLableVal.Content.ToString();
            }
            catch (Exception)
            {
                MessageBox.Show("Erreur");
            }
            try
            {
                this.mediaDao.saveNewMedia(this.media);
                MessageBox.Show( "Succès" );
            }
            catch (Exception)
            {
                MessageBox.Show( "Erreur" );
            }
            
        }

        private void PlayPreviwBtn_Click(object sender, RoutedEventArgs e) {
            this.elsList[0].Play();
        }

        private void PausePlayBtn_Click(object sender, RoutedEventArgs e) {
            this.elsList[0].Pause();
        }

        private void RestartPlayBtn_Click(object sender, RoutedEventArgs e) {
            this.elsList[0].Position = TimeSpan.Zero;
            this.PausePlayBtn_Click(null, null);
        }

        public void Dispose() {
            this.elsList[0].Stop();
            this.elsList[0].Source = null;
            this.elsList[0].Close();
            this.elsList.Clear();
            //GC.SuppressFinalize(this.elsList[0] );
        }

        public void setDuration(Boolean boll, MediaElement element) {
            if ( boll == true ) {
                element.MediaOpened += (s, e) => {
                    if ( element.NaturalDuration.HasTimeSpan ) {
                        TimeSpan duration = element.NaturalDuration.TimeSpan;
                        this.DureeLableVal.Content = duration;

                    }
                };
            } else {
                this.DureeLableVal.Content = "";
            }

        }

    }
}
